---@class Battle.EntityAiSystem:Battle.EntityComponent
local EntityAiSystem = Battle.CreateClass("EntityAiSystem",Battle.EntityComponent)
Battle.EntityAiSystem = EntityAiSystem
function EntityAiSystem:OnCreate()
    ---@type Battle.AiBase
    self.ai = nil
    self:RegUpdate(true)
end
function EntityAiSystem:OnDestroy()
    if self.ai then
        self:AiToPool(self.ai)
    end
    self.ai = nil
end
function EntityAiSystem:OnInitializedAfter()
    self:AddAi(self.actor,self:GetAi(self.actor.actorType))
end
function EntityAiSystem:Stop()
    if not self.ai then return end
    self.ai:Stop()
end
function EntityAiSystem:Update(dt)
    if self.actor.isDead then return end
    self.ai:BaseUpdate(dt)
end
function EntityAiSystem:GetBlackboardValue(name)
    return self.ai.tree.blackboard:Get(name)
end
function EntityAiSystem:SetBlackboardValue(name, value)
    return self.ai.tree.blackboard:Set(name,value)
end

---@param actor Battle.Pawn
function EntityAiSystem:AddAi(actor,aiType)
    if not actor then return end
    if self.ai then
        self:AiToPool(self.ai)
    end
    self.ai = nil
    if aiType then
        local ai = self:CreateAi(aiType)
        ai:BaseCreate(actor)
        self.ai = ai
    end
end
---@return Battle.AiBase
function EntityAiSystem:CreateAi(aiType)
    return Battle.PoolMgr.GetAiFromPool(aiType)
end
---@param ai Battle.AiBase
function EntityAiSystem:AiToPool(ai)
    ai:BaseDestroy()
    return Battle.PoolMgr.ReturnAiToPool(ai)
end
function EntityAiSystem:GetAi(actorType)
    if actorType == Battle.ActorType.Lord then
        return Battle.AiType.LordAi
    else
        return Battle.AiType.CommonAi
    end
end